//
//  TT_MessageManager.m
//  BaseProject
//
//  Created by 王洋 on 2020/11/4.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "TT_IMManager.h"
#import "JPUSHService.h"

#import "TT_MessageManager.h"

@implementation TT_MessageManager

+ (TT_MessageManager *)manager {
    
    static TT_MessageManager *shareInstance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        shareInstance = [TT_MessageManager new];
    });
    return shareInstance;
}

- (void)refreshUnReadCount {
    
    [self getTotalUnReadMessage:^(int unReadMsgCount) {

    }];
}

- (void)getTotalUnReadMessage:(void (^)(int unReadMsgCount))comption {
    
    int imCount = [[TT_IMManager sharedInstance] TTIM_GetUnreadMsgCount];
    self.IMUnReadCount = imCount;

    [self getSysUnReadMessage:^(int unReadMsgCount) {
            
        self.totalUnReadCount = imCount + unReadMsgCount;
        [self setbadge:self.totalUnReadCount];
        comption(self.totalUnReadCount);

    }];
}

- (void)getIMUnReadMessage:(void (^)(int))comption {
    int count = [[TT_IMManager sharedInstance] TTIM_GetUnreadMsgCount];
    self.IMUnReadCount = count;
    comption(count);
}

- (void)getSysUnReadMessage:(void (^)(int))comption {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];
    [dic setValue:@"1" forKey:@"receiveType"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_UnReadSysMsgCount];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    int notReadCount = [[resultDic valueForKey:@"notReadCount"] intValue];
                    
                    if (notReadCount < 0) {
                        notReadCount = 0;
                    }
                    weakSelf.SysUnReadCount = notReadCount;
                    comption(weakSelf.SysUnReadCount);
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {

    }];
}

- (void)getInteractionCount:(void (^)(void))comption {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_SysInteractionCount];
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    weakSelf.commentCount = [[resultDic valueForKey:@"commentCount"] intValue];
                    weakSelf.ZanCount = [[resultDic valueForKey:@"isZanCount"] intValue];
                    weakSelf.InteractionCount = weakSelf.commentCount + weakSelf.ZanCount;
                    comption();
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {

    }];
}

-(void)setbadge:(NSInteger)badge {
    [JPUSHService setBadge:badge]; //同步本地角标值到服务器

    //开启用户权限，否则不会显示
    NSString * version = [UIDevice currentDevice].systemVersion;
        if ([version floatValue] >= 8.0) {
            UIUserNotificationSettings * set = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:set];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    //设置显示数字
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
}

@end
